﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation.AdminPresentation
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        private Label _nameLabel;
        private LinkButton _logOffLink;
        private LinkButton _changePasswordAdmin;

        protected void Page_Init(object sender, EventArgs e)
        {
            _nameLabel = (Label)AdminMenu.FindControl("NameLabel");
            _logOffLink = (LinkButton)AdminMenu.FindControl("LogOffLink");
            _changePasswordAdmin = (LinkButton)AdminMenu.FindControl("ChangePasswordAdmin");
            _logOffLink.Click += new EventHandler(LogOffLink_Click);
            _changePasswordAdmin.Click += new EventHandler(ChangePassword_Click);
        }

        protected void LogOffLink_Click(object sender, EventArgs eventArgs)
        {
            Session["user"] = null;
            Response.Redirect("../LogOn.aspx");
        }

        protected void ChangePassword_Click(object sender, EventArgs eventArgs)
        {
            Response.Redirect("../Admin/ChangePasswordAdmin.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                Welcome();
            }
        }

        private void Welcome()
        {
            try
            {
                DateTime now = DateTime.Now.ToLocalTime();
                int checkTime = Int32.Parse(now.ToString("HH", CultureInfo.CurrentCulture), CultureInfo.CurrentCulture);
                string welcomeText = "";
                if (checkTime < 10)
                {
                    welcomeText = "Good morning";
                }
                else if (checkTime < 13)
                {
                    welcomeText = "Good lunch";
                }
                else if (checkTime < 18)
                {
                    welcomeText = "Good afternoon";
                }
                else if (checkTime < 22)
                {
                    welcomeText = "Good evenning";
                }
                else
                {
                    welcomeText = "Good night";
                }

                User user = (User)Session["user"];
                _nameLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0} {1}<br/>Role: {2}", welcomeText, user.UserName.ToUpper(CultureInfo.CurrentCulture), user.Role.RoleName.ToUpper(CultureInfo.CurrentCulture));
            }
            catch (NullReferenceException exception)
            {
                ErrorLabel.Text = exception.Message;
            }
            catch (ArgumentException exception)
            {
                ErrorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                ErrorLabel.Text = exception.Message;
            }
        }
    }
}